using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net.Mail;
using System.Net;

public partial class contact : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if(Request.UrlReferrer != null)
                hdnPrevious.Value = Request.UrlReferrer.ToString();
        }
        if(Request.QueryString["b"] != null)
        {
            phBooking.Visible = true;
        }
    }
    protected void Submit_ServerClick(object sender, EventArgs e)
    {
        string msg_body = body.Value;

        if (msg_body != null)
        {
            //string from_emailTo = DropDownList1.SelectedValue;
            //string from_email = email.Value;
            string from_emailTo = ConfigurationManager.AppSettings["EmailAddressTo"].ToString();
            string from_email = ConfigurationManager.AppSettings["EmailAddressFrom"].ToString();
            string from_name = name.Value;
            string client_email = email.Value;
            string from_company = company.Value;
            string from_title = title.Value;
            string from_tel = telephone.Value;
            string previous = hdnPrevious.Value.ToString();
            //string msg = String.Format("<p><strong>Email from:</strong> {0} ({1})<br />Company:{5}<br />Title:{6}<br /><br /><strong>Message:</strong><p>{2}</p><strong>Tel:</strong> {3}<br /><strong>Previous page:</strong> {4}</p>", from_name, client_email, msg_body, from_tel, previous, from_company, from_title);

            //if (people.Value.Length > 0)
            //{
            //    msg += String.Format("<p><strong>Number of people:</strong> {0}<br /><strong>Booking Date:</strong> {1}<br /><strong>Booking Time:</strong> {2}</p>", people.Value, bookingDate.Value, bookingTime.Value);
            //}


            string msg = String.Format(@"
Email from: {0} ({1})
Company:{5}
Title:{6}

Message:
{2}
Tel: {3}
Previous page: {4}
", from_name, client_email, msg_body, from_tel, previous, from_company, from_title);

            //if (people.Value.Length > 0)
            //{
            //    msg += String.Format(@"Number of people:</strong> {0}<br /><strong>Booking Date:</strong> {1}<br /><strong>Booking Time:</strong> {2}</p>", people.Value, bookingDate.Value, bookingTime.Value);
            //}

            try
            {
                SmtpClient mailClient = new SmtpClient("smtp.gmail.com", 587);
                mailClient.EnableSsl = true;
                NetworkCredential cred = new NetworkCredential("hosetoyssender@gmail.com", "patrickhose");
                mailClient.Credentials = cred;
                mailClient.Send(from_email, from_emailTo, ConfigurationManager.AppSettings["EmailSubject"].ToString(), msg);
                Response.Redirect("thanks.aspx");
            }
            catch (Exception)
            {
                lblMSG.Text = "Failed to send email!";
            }
            //if (Email.SendMail(from_email, from_emailTo, ConfigurationManager.AppSettings["EmailSubject"].ToString(), msg, true))
            //{
            //    Response.Redirect("thanks.aspx");
            //}
            //else
            //{
            //    lblMSG.Text = "Failed to send email!";
            //}
        }
        else
        {
            lblMSG.Text = "Please add a message!";
        }
    }
}
